﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES.Modele;
using Persistence.OPManager;

namespace GestionPedago.Services.Mapping
{
    /// <summary>
    /// Classe de mapping Entity<->ES de l'objet Contact
    /// </summary>
    public class ContactMappeur : IMappeur<Contact, ContactES>
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public ContactMappeur() { }

        /// <summary>
        /// Mapping Entity -> ES
        /// </summary>
        /// <param name="entity">L'entity à mapper</param>
        /// <returns>L'ES obtenu</returns>
        public ContactES Entity2ES(Contact entity)
        {
            ContactES retour = new ContactES();
            retour.CodeContact = entity.CodeContact;
            retour.Nom = entity.Nom;
            retour.Prenom = entity.Prenom;
            retour.TelFixe = entity.TelFixe;
            retour.TelMobile = entity.TelMobile;
            retour.Fax = entity.Fax;
            retour.Email = entity.Email;
            retour.CodeFonction = entity.CodeFonction;
            retour.Observation = entity.Observation;
            retour.CodeEntreprise = entity.CodeEntreprise;
            retour.CodeImportance = entity.CodeImportance;
            retour.Archive = entity.Archive;
            retour.Civilite = entity.Civilite;
            EntrepriseMappeur entrepriseMappeur = new EntrepriseMappeur();
            retour.Entreprise = entrepriseMappeur.Entity2ES(entity.Entreprise);
            FonctionMappeur fonctionMappeur = new FonctionMappeur();
            retour.Fonction = fonctionMappeur.Entity2ES(entity.Fonction);
            return retour;
        }

        /// <summary>
        /// Mapping ES -> Entity
        /// </summary>
        /// <param name="es">L'ES à mapper</param>
        /// <returns>L'entity obtenu</returns>
        public Contact ES2Entity(ContactES es)
        {
            Contact retour = new Contact();
            retour.CodeContact = es.CodeContact;
            retour.Nom = es.Nom;
            retour.Prenom = es.Prenom;
            retour.TelFixe = es.TelFixe;
            retour.TelMobile = es.TelMobile;
            retour.Fax = es.Fax;
            retour.Email = es.Email;
            retour.CodeFonction = es.CodeFonction;
            retour.Observation = es.Observation;
            retour.CodeEntreprise = es.CodeEntreprise;
            retour.CodeImportance = es.CodeImportance;
            retour.Archive = es.Archive;
            retour.Civilite = es.Civilite;
            if (es.Entreprise != null)
            {
                retour.Entreprise = EntrepriseOPManager.GetEntrepriseByCode(es.CodeEntreprise.Value);
            }
            FonctionMappeur fonctionMappeur = new FonctionMappeur();
            retour.Fonction = fonctionMappeur.ES2Entity(es.Fonction);
            if (!EntityStateChecker.IsDetached(retour))
            {
                EntityConnection.GetContext().Contact.ApplyCurrentValues(retour);
            }
            return retour;
        }
    }
}
